package com.penghaisoft.microwms.base.web.data.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.penghaisoft.microwms.base.web.data.service.IMdPurchaseService;
import com.penghaisoft.microwms.base.web.purchase.dao.MdPurchaseMapper;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.data.model.MdPurchase;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;

@Service("mdPurchaseService")
public class MdPurchaseServiceImpl extends BaseService implements IMdPurchaseService {
	@Resource
	private MdPurchaseMapper mdPurchaseMapper;
	
	@Override
	public Resp create(MdPurchase mdPurchase) {
		MdPurchase mp = new MdPurchase();
//		Long count = mdPurchaseMapper.queryCount(mp) + 1;
//		mdPurchase.setPurchaseCode(count.toString());
		mdPurchaseMapper.create(mdPurchase);
		return success();
	}

	@Override
	public Resp delete(MdPurchase mdPurchase) {
		mdPurchaseMapper.delete(mdPurchase);
		return success();
	}

	@Override
	public Pager<MdPurchase> findListByCondition(int page, int rows, MdPurchase condition) {
		Pager<MdPurchase> pager = new Pager<>();
	    pager.setPage(page);
	    pager.setRows(rows);
//		condition.preQuery();
		long size = mdPurchaseMapper.queryCount(condition);
		List<MdPurchase> records = new ArrayList<>();
		if(size > 0){
			records = mdPurchaseMapper.queryList(pager,condition);
		}
		pager.setRecords(records);
		pager.setTotalCount(size);
		return pager;
	}

	@Override
	public MdPurchase findById(String id) {
		return mdPurchaseMapper.queryById(id);
	}

	@Override
	public Resp update(MdPurchase mdPurchase) {
		Resp resp = new Resp();
		int count = mdPurchaseMapper.checkUpdUnique(mdPurchase);
    	if(count > 0){
    		resp.setCode(Constant.FAIL);
    		resp.setMsg("货主名称冲突");
    		return resp;
    	}
		mdPurchaseMapper.updateBySelect(mdPurchase);
		return success();
	}

	@Override
	public TransResult queryPurchase() {
		TransResult result = new TransResult();
		List<MdPurchase> list = mdPurchaseMapper.queryPurchase();
		result.setCode(Constant.SUCCESS);
		result.setData(list);
		return result;
	}

	@Override
	public boolean existence(MdPurchase mdPurchase) {
		Long count =mdPurchaseMapper.queryCount1(mdPurchase);
		if(count > 0)
			return true;
		else 
			return false;
	}

}
